package com.cdu.aspect;

import com.cdu.annotation.OperationLogAnnotation;
import com.cdu.async.LogAsync;
import com.cdu.model.common.BaseResponse;
import com.cdu.pojo.SysLog;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Date;

/**
 * projectName : app3_spring2ibatis
 * className: OperationLogAOP
 * description: TODO 类描述
 *
 * @author : git.jas0nch
 * date: 2022-03-28
 */

@Component
@Aspect
@Slf4j
public class OperationLogAOP {

    @Autowired
    private LogAsync logAsync;


    /**
     * @param
     * @return : void
     * @description : 配置织入点
     * @author : git.jas0nch
     * date : 2022/3/28
     **/
    @Pointcut("@annotation(com.cdu.annotation.OperationLogAnnotation)")
    public void logPointCut() {

    }

    @AfterReturning(pointcut = "logPointCut()&&@annotation(operationLogAnnotation)", returning = "jsonData")
    public void afterReturn(JoinPoint point, Object jsonData, OperationLogAnnotation operationLogAnnotation) {
        handleLog((HttpServletRequest) point.getArgs()[0], (BaseResponse) jsonData, operationLogAnnotation);
    }

//    @AfterThrowing(pointcut = "logPointCut()&&@annotation(operationLogAnnotation)", throwing = "e")
//    public void afterThrowingReturn(JoinPoint point, Exception e, OperationLogAnnotation operationLogAnnotation) {
//        HttpServletRequest request = (HttpServletRequest) point.getArgs()[0];
//        handleLog(e, null, operationLogAnnotation);
//    }


    protected void handleLog(HttpServletRequest request, BaseResponse jsonData, OperationLogAnnotation operationLogAnnotation) {

        try {
            OperationLogAnnotation controllerLog = operationLogAnnotation;
            if (controllerLog == null) {
                log.info("the annotation is null here about the log");
                return;
            }
            // set the data here;
            JSONObject jsonObject = JSONObject.fromObject(jsonData.getData());
            SysLog sysLog = new SysLog();
            sysLog.setCreateTime(new Date());
            // TODO 这里还没想好如何处理数据。
            if (jsonObject == null) {
                log.info("the jsonObject is null here about the log");
            } else {
                sysLog.setData(jsonData.toString().getBytes(StandardCharsets.UTF_8));
            }
            sysLog.setIp(request.getRemoteAddr());
            sysLog.setUrl(request.getRequestURI());
            sysLog.setDescription(controllerLog.getOperationAction().getMessage());
            logAsync.addOperationLog(sysLog);
        } catch (Exception e) {
            log.error("糟糕，发生异常了，异常：{}", e.getMessage());
            e.printStackTrace();
        }

    }

}
